package com.lovo.csc.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.commom.WebRemote.IWebOrderRemote;
import com.lovo.commom.WebRemote.IWebUserRemote;
import com.lovo.commom.util.ConstantUtil;
import com.lovo.csc.bean.OrderInfoBean;
import com.lovo.csc.bean.RefundBean;
import com.lovo.csc.bean.UserBean;
import com.lovo.csc.bean.UserOrderBean;
import com.lovo.csc.mapper.IOrderInfoMapper;
import com.lovo.csc.mapper.IRefundMapper;
import com.lovo.csc.mapper.IUserMapper;
import com.lovo.csc.mapper.IUserOrderMapper;
import com.lovo.csc.service.IUserOrderService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;

@Service
@Transactional
public class UserOrderServiceImpl implements IUserOrderService {


    @Autowired
    private IUserOrderMapper userOrderMapper;
    @Autowired
    private IRefundMapper refundMapper;
    @Autowired
    IUserMapper userMapper;
    @Autowired
    IWebUserRemote webUserRemote;
    @Autowired
    IWebOrderRemote webOrderRemote;

    @Autowired
    IOrderInfoMapper orderInfoMapper;


    @Override
    public void updateStatus(long userOrderId) {
        QueryWrapper<UserOrderBean> qw = new QueryWrapper<>();
        qw.eq("pk_userOrderId", userOrderId);
        UserOrderBean userOrderBean = userOrderMapper.selectOne(qw);
        userOrderBean.setStatus(3);
        userOrderMapper.updateById(userOrderBean);
        //修改退款详情表状态
        QueryWrapper<RefundBean> qw2 = new QueryWrapper<>();
        qw2.eq("fk_userOrderIdNum", userOrderBean.getOrderNum());
        RefundBean refundBean = refundMapper.selectOne(qw2);
        //退款
        QueryWrapper<UserBean> qw3 = new QueryWrapper<>();
        qw3.eq("uo_userName", userOrderBean.getUserName());
        UserBean user = userMapper.selectOne(qw3);
        user.setMoney(user.getMoney() + userOrderBean.getBuyPrice());
        userMapper.updateById(user);
        //远程修改金额
        webUserRemote.addMoney(user.getUserName(),userOrderBean.getBuyPrice());
        //审核通过
        refundBean.setAuditState(1);
        refundMapper.updateById(refundBean);
        //远程调用商城后端修改订单状态接口
        webOrderRemote.updateStatusRemote(userOrderBean.getOrderNum(),6);
    }

    @Override
    public void updateStatus(String orderIdNum, int status) {
        QueryWrapper<UserOrderBean> qw = new QueryWrapper<>();
        qw.eq("uo_idNum", orderIdNum);
        UserOrderBean userOrderBean = userOrderMapper.selectOne(qw);
        userOrderBean.setStatus(status);
        userOrderMapper.updateById(userOrderBean);
    }

    @Override
    public IPage<UserOrderBean> findAllNoRefund(int pageNO) {
        return userOrderMapper.findAllNoRefund(new Page<>(pageNO, ConstantUtil.SIZE));
    }

    @Override
    public boolean CscUpdateStatus(String orderIdNum, int status, LocalDate payDate) {
        try {
            QueryWrapper<UserOrderBean> qw = new QueryWrapper<>();
            qw.eq("uo_idNum", orderIdNum);
            UserOrderBean userOrderBean = userOrderMapper.selectOne(qw);
            userOrderBean.setStatus(status);
            if (payDate != null){
                userOrderBean.setPayDate(payDate);
            }
            if (status == 5){
                ConstantUtil.WS_MSG.add("客户订单审核");
            }
            userOrderMapper.updateById(userOrderBean);

            return true ;
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    @RabbitListener(queues = "orderToCscQueue")
    public void getUserInfo(String json) {
        UserOrderBean orderBean = JSONObject.parseObject(json, UserOrderBean.class);
        userOrderMapper.insert(orderBean);
    }

    /**
     * 提供给后端供应商退货的接口
     */
    @Override
    public void updateStatusTo3(String idNum) {
        QueryWrapper<OrderInfoBean> qw=new QueryWrapper<>();
        qw.eq("oi_idNum",idNum);
        OrderInfoBean orderInfo=orderInfoMapper.selectOne(qw);
        orderInfo.setOrderState(3);
        orderInfoMapper.updateById(orderInfo);
    }
}
